#include <vector>
using namespace std;
/*
 * @lc app=leetcode.cn id=335 lang=cpp
 *
 * [335] 路径交叉
 */

// @lc code=start
class Solution {
public:
    
    bool isSelfCrossing(vector<int>& distance) {
        if (distance.size() < 4) return false;
        for (int i = 3; i < distance.size(); i++) {
            // 第一种相交的情况
            if (distance[i] >= distance[i - 2] && distance[i - 1] <= distance[i - 3]) return true;
            if (i == 4 && distance[i] + distance[i - 4] >= distance[i - 2] && distance[i - 1] == distance[i - 3]) return true;
            if (i >= 5 && distance[i] + distance[i - 4] >= distance[i - 2] && 
                distance[i - 1] + distance[i - 5] >= distance[i - 3]
                && distance[i - 2] > distance[i - 4]
                && distance[i - 1] <= distance[i - 3]) return true;
        }
        return false;
    }
};
// @lc code=end

